package com.jin_mo.custom.download;

import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloadQueueSet;
import com.liulishuo.filedownloader.FileDownloader;

public class FileDownloadManager {

    /**
     *
     * @param url
     * @param fileName 保存的文件路径
     */
    public static void downloadFile(String url, String fileName, DownloaderListener listener) {
        if (listener != null) {
            listener.onProgress(0);
        }
        FileDownloadListener fileDownloadListener = new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                Log.d("fileDownloader===>" ,"pending"  );
            }

            @Override
            protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
                Log.d("fileDownloader===>" ,"connected etag==" +etag + "isContinue==" +isContinue );
            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                LogUtils.d(soFarBytes,totalBytes);
                double progress = ((double) soFarBytes / (double) totalBytes) * 100;
                Log.d("fileDownloader===>" ,"progress===" +progress  );
                if (listener != null) {
                    listener.onProgress((int)Math.floor(progress));
                }

            }

            @Override
            protected void blockComplete(BaseDownloadTask task) {
                Log.d("fileDownloader===>" ,"blockComplete"  );

            }

            @Override
            protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
            }

            @Override
            protected void completed(BaseDownloadTask task) {
                Log.d("fileDownloader===>" ,"completed"  );
                if (listener != null) {
                    listener.onCompleted(task);
                }
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
                Log.d("fileDownloader===>" ,"error"  +e.getMessage() );
                if (listener != null) {
                    listener.onError(e.getMessage());
                }
            }

            @Override
            protected void warn(BaseDownloadTask task) {
                Log.d("fileDownloader===>" ,"warn"   );

                if (listener != null) {
                    listener.warn(task);
                }
            }
        };
        BaseDownloadTask baseDownloadTask = FileDownloader.getImpl().create(url).setTag(url).setPath(fileName);
//        FileDownloader.getImpl().create(url)
//                .setPath(fileName)
//                .start(fileDownloadListener,false);
        FileDownloadQueueSet queueSet = new FileDownloadQueueSet(fileDownloadListener);
        queueSet.setAutoRetryTimes(1);
        // 并行执行该任务队列
        queueSet.downloadTogether(baseDownloadTask);
        queueSet.start();
    }


}
